package com.kun.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kun.dao.BookInfoDao;
import com.kun.dao.PutbookInfoDao;
import com.kun.entity.BookInfo;
import com.kun.entity.PutbookInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BookInfoService {
    @Autowired
    private BookInfoDao bookInfoDao;

    public BookInfo findById(Integer putbookId) {
        return bookInfoDao.selectByPrimaryKey(putbookId);
    }

    public Map<String, Object> selectByPage(Integer pageNum, Integer pageSize, BookInfo bookInfo) {
        //通过PageHelper对象设定分页参数
        PageHelper.startPage(pageNum, pageSize);
        //当前页面的数据
        List<BookInfo> list = bookInfoDao.findAll(bookInfo);
        //创建PageInfo对象，并将当前页面数据传入，计算分页相关数据
        PageInfo<BookInfo> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("total", total);
        return map;
    }
    public int insertBook(BookInfo bookInfo) {
        return bookInfoDao.insert(bookInfo);
    }
    public int updateBook(BookInfo bookInfo){
        return bookInfoDao.updateByPrimaryKeySelective(bookInfo);
    }
    public List<BookInfo> exportExcel(BookInfo bookInfo) {
        return bookInfoDao.findAll(bookInfo);
    }
    public int deleteBook(Integer[] putbookIds){return  bookInfoDao.batchDelete(putbookIds);}
}
